package com.zds.hj.poseidon.biz.dispatcher.credit;

import com.alibaba.fastjson.JSON;
import com.zds.boot.dispatch.comm.constants.DispatcherConstant;
import com.zds.boot.dispatch.comm.enums.ApproveStageEnum;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.*;
import com.zds.boot.dispatch.spi.norm.DispatchBizInterface;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author duanhao
 * @Description TODO
 * @Date 2023/5/25 9:34
 */
public abstract class TestAbsHandler implements DispatchBizInterface {


    @Override
    public BaseResultModel doExecute(BaseReqModel params) {
        Object reqData = (Object) this.covertObject(params);
        // TODO: 2023/5/24 根据bizNo查询数据库数据
//        CreditInfoEntityDto dbData = findCreditApply(params);
//        CreditInfoEntityDto dbData = null;
//        reqData.setId(dbData.getId());
//        reqData.setProductType(dbData.getProductType());
//        reqData.setMerchantNo(params.getMerchantNo());
//        reqData.setChannelNo(params.getChannelNo());


        BaseResultModel resultModel = defaultResult(params);
        //调用具体的handler的执行逻辑
        this.creditDoExecute(params,resultModel,reqData, new Object());

        // TODO: 2023/5/24 更新步骤信息
//        creditApplyService.update(params, dbData);
        return resultModel;
    }

    @Override
    public Boolean skipThisStep(BaseReqModel params) {
        return false;
    }

    @Override
    public Object preHanding(BaseReqModel params) {
        return null;
    }

    @Override
    public Object redoPreHanding(BaseReqModel params) {
        return preHanding(params);
    }

    @Override
    public BaseResultModel doSection(BaseReqModel baseReqModel) {
        return defaultResult(baseReqModel);
    }

    @Override
    public Object echoDetail(BaseReqModel params) {
        return null;
    }

    @Override
    public Object echoList(BaseReqModel params) {
        return DispatcherConstant.ECHO_LIST_DEFAULT_RETURN;
    }

    @Override
    public Object assessStep(BaseReqModel params) {
        return DispatcherConstant.ASSESS_STEP_DEFAULT_RETURN;
    }

    public Object covertJsonObj2Obj(BaseReqModel params) {
        if (params.getRequest() != null) {
            return JSON.parseObject(JSON.toJSONString(params.getRequest()), Object.class);
        }
        return new Object();
    }

    @Override
    public Object covertObject(BaseReqModel reqModel) {
        return covertJsonObj2Obj(reqModel);
    }

    @Override
    public DispatchInitConfig dispatchInit() {
        DispatchInitConfig config = new DispatchInitConfig();
        config.setModuleTags(moduleTags());
        config.setInstructRegisterModules(moduleInstructs());
        config.setModuleBaseInfo(moduleBaseInfo());
        config.setModuleDeveloperInfo(initModuleDeveloperInfo(new ModuleDeveloperInfo()));
        return config;
    }

    /**
     * @return
     */
    public ModuleTag moduleTags() {
        ModuleTag moduleTag = new ModuleTag();
        moduleTag.setStageTag(ApproveStageEnum.CREDIT);
        moduleTag.setContractAuth(false);
        return moduleTag;
    }

    public abstract int getSequenceTag();

    /**
     * @Author jingyh
     * @Description 指令集
     * @param
     * @return java.util.List<com.zds.boot.dispatch.dto.InstructRegisterModule>
     **/
    public List<InstructRegisterModule> moduleInstructs() {
        return new ArrayList<>();
    }

    public ModuleDeveloperInfo initModuleDeveloperInfo(ModuleDeveloperInfo moduleDeveloperInfo){
        // handler需要设置的自己重写它
        return moduleDeveloperInfo;
    }

    /**
     * 定义模块基本信息(默认)，特殊需重写
     * @return
     */
    public ModuleBaseInfo moduleBaseInfo() {
        ModuleBaseInfo info = new ModuleBaseInfo();
        info.setNodeType(ModuleNodeTypeEnum.BUSINESS);
        info.setReverse(true);
        info.setAuto(false);
        info.setAsyn(false);
        return info;
    }

    /* *
     *========================================
     * @方法说明 ： 默认返回模组
     * @exception
     * @创建时间：     2019/7/25 17:54
     *========================================
     */
    public BaseResultModel defaultResult(BaseReqModel params) {
        BaseResultModel resultModel = new BaseResultModel();
        resultModel.setProductId(params.getProductId());
        resultModel.setStage(params.getStage());
        resultModel.setChannelNo(params.getChannelNo());
        resultModel.setMerchantNo(params.getMerchantNo());
        resultModel.setModuleFlowType(params.getModuleFlowType());
        return resultModel;
    }
    /* *
     *========================================
     * @方法说明 ： 授信特有的 执行方法一般由顶层发起调用
     * @author : shihongwei
     * @param params
     * @param creditInfoEntityDto
     * @return      void
     * @exception
     * @创建时间：     2019/8/3 16:05
     *========================================
     */
    public abstract void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, Object reqData, Object dbData);


    @Override
    public Object paramsFerry(Object o) {
        Object dbDto = new Object();
        return dbDto;
    }

//    public static void main(String[] args) {
//        List<String> strings = Arrays.asList("aaa", "bbb", "ccc", "ddd");
//
//        System.out.println(Math.random());
//
//        int v = (int )(Math.random() * strings.size());
//
//        System.out.println(v);
//    }



}
